<?php
$this->breadcrumbs=array(
	'Homepage'=>array('index'),
	'Manage',
);?>

<div class="main-tit">
    <?php echo CHtml::link('Create Banner', array('banner/create'), array('class'=>'btn-default')) ?>
    <h2>Manage Banner</h2>
</div>

<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'banner-grid',
	'dataProvider'=>$modelBanner->search(),
	'filter'=>$modelBanner,
    'cssFile'=>Yii::app()->baseUrl.'/css/admin/gridview/styles.css',
    'pager'=>array(
        'cssFile'=>Yii::app()->baseUrl.'/css/admin/pager.css',
    ),
    'template'=>'{items}{summary}{pager}',
	'columns'=>array(
        array(
			'name'=>'name',
			'filter'=>false,
		),
		'url',
		array(
			'name'=>'create_time',
			'type'=>'datetime',
		),
        array(
			'name'=>'update_time',
			'type'=>'datetime',
		),
		array(
			'class'=>'CButtonColumn',
            'template' => '{view} {update} {delete}',
            'viewButtonImageUrl'=>Yii::app()->baseUrl.'/css/admin/gridview/view.png', 
            'deleteButtonImageUrl'=>Yii::app()->baseUrl.'/css/admin/gridview/delete.png',
            'buttons'=>array(
                'update'=>array(
                    'imageUrl'=>Yii::app()->baseUrl.'/css/admin/gridview/update.png',
                    'click'=>"function(e){
                        e.preventDefault();
                        $('#update-modal').children(':eq(0)').empty(); // Stop auto POST
                        updateModal($(this).attr('href'));
                        $('#update-modal')
                            .modal({ title:'Update Banner' })
                            .modal('show');
                    }",
                ),
                'delete' => array(
                    'click' => "function( e ){
                        e.preventDefault();
                        $('#update-modal').children(':eq(0)').empty(); // Stop auto POST
                        updateModal($(this).attr('href'));
                        $('#update-modal')
                            .modal({ title:'Delete confirmation' })
                            .modal('show');
                    }",
                ),
            ),
		),
	),
)); ?>

<!-- Modal for Creating Banner -->
<?php $this->beginWidget('zii.widgets.jui.CJuiModal', array(
    'id' => 'modalBanner',
    'options' => array(
        'title' => 'Create Banner',
        'autoOpen' => false,
        'modal' => true,
        'width' => 410,
        'height' => 280,
        'resizable'=>false,
    ),
)); ?>
<div class="bannerForm"></div>
<?php $this->endWidget(); ?>

<script type="text/javascript">
// Add a banner by ajax
function addBanner()
{
    <?php echo CHtml::ajax(array(
        'url'=>array('banner/create'),
        'data'=> "js:$(this).serialize()",
        'type'=>'post',
        'dataType'=>'json',
        'success'=>"function(data)
        {
            if (data.status == 'failure')
            {
                $('#modalBanner div.bannerForm').html(data.content);
                // Here is the trick: on submit-> once again this function!
                $('#modalBanner div.bannerForm form').submit(addBanner);
            }
            else
            {
                //console.log(data);
                $('#modalBanner div.bannerForm').html(data.content);
                setTimeout(\"$('#modalBanner').modal('hide') \",2000);
                setTimeout(\"$.fn.yiiGridView.update('banner-grid', { data: $(this).serialize() }) \",2000);
            }

        } ",
    )); ?>
    return false; 
}

</script>

<!-- Modal for Update -->
<?php
$this->beginWidget( 'zii.widgets.jui.CJuiModal', array(
    'id' => 'update-modal',
    'options' => array(
        'title' => 'Modal',
        'autoOpen' => false,
        'width' => 410,
        'height' => 280,
        'resizable' => false,
    ),
)); ?>
<div class="update-modal-content"></div>
<?php $this->endWidget(); ?>

<?php
$updateJS = CHtml::ajax(array(
    'url' => "js:url",
    'data' => "js:form.serialize() + action",
    'type' => 'post',
    'dataType' => 'json',
    'success' => "function(data)
    {
        if(data.status == 'failure')
        {
            $('#update-modal div.update-modal-content').html(data.content);
            $('#update-modal div.update-modal-content form input[type=submit]')
                .die() // Stop from re-binding event handlers
                .live('click', function(e){ // Send clicked button value
                e.preventDefault();
                updateModal(false, $(this).attr('name'));
            });
        }
        else
        {
            $('#update-modal div.update-modal-content').html(data.content);
            if(data.status == 'success') // Update all grid views on success
            {
                $('div.grid-view').each(function(){ // Change the selector if you use different class or element
                    $.fn.yiiGridView.update($(this).attr('id'));
                });
            }
            setTimeout(\"$( '#update-modal').modal('hide').children(':eq(0)').empty();\", 1000);
        }
    }"
)); ?>

<?php
Yii::app()->clientScript->registerScript('updateModal', "function updateModal(url, act)
    {
        var action = '';
        var form = $('#update-modal div.update-modal-content form');
        if(url == false)
        {
            action = '&action=' + act;
            url = form.attr('action');
        }
        {$updateJS}
    }" 
); ?>
